<!DOCTYPE HTML>
<html class="butler">
<head>
    <title>科寓共享家</title>
    <% include common/meta.ejs %>
    <% include common/public.ejs %>
    <% let stateMap = {
        '1': '待处理',
        '0': '全部'
    }%>
    <style>
        #genBillBtn{
            background-color: #04BE02;
            color: #ffffff;
            width: auto;
            padding: 4px;
        }
    </style>
</head>
<body>
<div class="weui_tab">
    <div class="weui_navbar">
        <a class="weui_navbar_item conditionState">
            <%=stateMap[data.condition.properties.status]%>
            <img class="downIcon" src="<%=global.config.publicPath %>/page/images/icon/butler/down24.png" />
        </a>
        <a class="weui_navbar_item conditionMonth">
            <%=data.condition.properties.month%>
            <img class="downIcon" src="<%=global.config.publicPath %>/page/images/icon/butler/down24.png" />
        </a>
    </div>
    <div class="weui_tab_bd">
        <% let list = data.list;%>
        <% for(var i = 0; i < list.length; i++){ %>
        <div class="block">
            <h3>
                <span class="stateIcon"></span>
                房间：<%=list[i].houseName%>(<%=list[i].properties.houseNumber%>)
            </h3>
            <p>
                <span class="label">上次抄表</span>
                <span><%=list[i].lastDate %></span>
                <input type="hidden" value="<%= list[0].lastDate.substring(5)%>" id="checkMonth">
            </p>
            <p>
                <span class="label">抄表数据</span>
                <span>
                    <%= list[i].lastMeterInfo%>
                </span>
            </p>
            <hr />
            <button type="button" data-id="<%=list[i].id %>" data-info="<%=list[i].houseName%>-<%=list[i].houseId%>-<%=list[i].code%>" class="btn btn-warning btn-sm" id="genBillBtn">
                <i class="fa fa-money"></i>
                生成账单
            </button>
            <button class="editIcon" data-id="<%=list[i].id %>" data-info="<%=list[i].houseName%>-<%=list[i].houseId%>-<%=list[i].code%>"></button>
            <h4><%=data.condition.month %></h4>
            <p style="height: 35px">

            </p>

          <!--  <p>
                <span class="label">抄表数据</span>
                <span>
                    水表:<%=list[i].water || ' - ' %> /
                    电表:<%=list[i].electric || ' - '  %> /
                    燃气:<%=list[i].gas || ' - '  %>
                </span>
            </p>-->
        </div>
        <% } %>

        <div id="editPopup" class="weui-popup-container  popup-bottom">
            <div class="weui-popup-overlay"></div>
            <div class="weui-popup-modal">
                <div class="toolbar">
                    <div class="toolbar-inner">
                        <a href="javascript:void(0);" class="picker-button" id="saveButton" style="z-index: 1000;">保存</a>
                        <h1 class="title"><%=data.condition.month %> 抄表单</h1>
                    </div>
                </div>
                <div class="modal-content">
                    <form id="editForm">
                        <div class="weui_cells weui_cells_form">
                            <div class="weui_cell">
                                <div class="weui_cell_hd"><label class="weui_label">水表</label></div>
                                <div class="weui_cell_bd weui_cell_primary">
                                    <input class="weui_input" type="number" placeholder="" name="water" />
                                </div>
                            </div>
                        </div>
                        <div class="weui_cells weui_cells_form">
                            <div class="weui_cell">
                                <div class="weui_cell_hd"><label class="weui_label">电表</label></div>
                                <div class="weui_cell_bd weui_cell_primary">
                                    <input class="weui_input" type="number" placeholder="" name="power" />
                                </div>
                            </div>
                        </div>
                        <div class="weui_cells weui_cells_form">
                            <div class="weui_cell">
                                <div class="weui_cell_hd"><label class="weui_label">燃气</label></div>
                                <div class="weui_cell_bd weui_cell_primary">
                                    <input class="weui_input" type="number" placeholder="" name="gas" />
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<% include common/footer.ejs %>
<script>
    'use strict';
    let editId = '', info, code;
    let sendData={
        house:{

        }
    }
    let loadFlag = false;
    let pageNumber = 0;
    function transCode() {
        let oDate = new Date(); //实例一个时间对象；
        let y=  oDate.getFullYear();   //获取系统的年；
        let m= oDate.getMonth()+1;   //获取系统月份，由于月份是从0开始计算，所以要加1
        let d = oDate.getDate(); // 获取系统日，
        let h=oDate.getHours(); //获取系统时，
        let mm = oDate.getMinutes(); //分
        let s = oDate.getSeconds(); //秒
        let random = parseInt(Math.random()*100);

        var rand = "";
        for(var i = 0; i < 3; i++){
            var r = Math.floor(Math.random() * 10);

            rand += r;

        }
        console.log(rand);
        return  `${y}-${m}-${d}T${h}:${mm}:${s}.${rand}`


    }

    $('.editIcon').click(function () {
        editId = $(this).data('id');
        console.log(editId);
        Dolphin.form.empty($('#editForm'));
        $('#editPopup').popup();
        info = $(this).data('info');
        console.log(info);
        sendData.code=code;
        sendData.name = info.split('-')[0];
        sendData.houseId = info.split('-')[1];
        code = info.split('-')[2];
        if(code){
            sendData.code = code;
        }else{
            sendData.code =  sendData.name+transCode();
        };

        sendData.filterFlag = 0;
    });

    $('#saveButton').click(function () {
        let data = Dolphin.form.getValue($('#editForm'));
        let id = $(this).data('id');
        sendData.id=editId;
        sendData.water = data.water;
        sendData.electric = data.power;
        sendData.gas = data.gas;
        let dd = new Date().getDate();
        let yy = new Date().getFullYear();
        sendData.meterDay = dd.toString();
        //错误,拿到的总是当前月份的
        let monthcheck=$('#checkMonth').val();
        let month=parseInt(monthcheck);
        sendData.meterMonth =month+1;
        sendData.meterYear = yy.toString();
        sendData.id= editId;
        console.log(id);
        console.log(sendData);
        Dolphin.ajax({
            url: CTX + '/api/509cb2a903d2466b9645432e02506141',
            type: Dolphin.requestMethod.PUT,
            forceUrl: 'true',
            data: Dolphin.json2string(sendData),
            onSuccess: function (data) {
                console.log(data);
                $.toast("保存成功", function () {
                    $.closePopup();
                    location.reload();
                });
            }
        });



//        Dolphin.ajax({
//            url:'/api/5cb78c416b674cab93f1c60f2c09d375',
//            data: Dolphin.json2string(data),
//            onSuccess: function (data) {
//                location.reload();
//            }
//        })
    });

    $('.conditionState').click(function () {
        $.actions({
            title: '请选择状态',
            actions: [{
                text: "待处理",
                className: 'color-warning',
                onClick: function() {
                    location.href = CTX + `/view/butlerInstrument?status=1&month=<%=data.condition.properties.month %>`;
                }
            },{
                text: "全部",
                className: 'color-success',
                onClick: function() {
                    location.href = CTX + `/view/butlerInstrument?status=0&month=<%=data.condition.properties.month %>`;
                }
            }]
        });
    });

    $('.conditionMonth').click(function () {
        $.actions({
            title: '请选择月份',
            actions: (function () {
                let items = [];
                let now = new Date();
                for(let i = 0; i < 3; i++){
                    let month = Dolphin.date2string(new Date(now.getFullYear(), now.getMonth() - i, 1), 'yyyy-MM');
                    items.push({
                        text: month,
                        onClick: function () {
                            location.href = CTX + `/view/butlerInstrument?status=<%=data.condition.properties.status %>&month=${month}`;
                        }
                    })
                }
                return items;
            })()
        });
    });

    //生成账单
    $('#genBillBtn').click(function () {
        let data={};
        info = $(this).data('info');
        data.code = info.split('-')[2];
        Dolphin.ajax({
            url: CTX + '/api/ff0163fa93fb490da256aef5b01b0844',
            type: Dolphin.requestMethod.PUT,
            forceUrl: 'false',
            data: Dolphin.json2string(data),
            onSuccess: function (reData) {
               console.log("---------end------------");
                $.toast("生成账单成功", function () {
                    location.reload();
                });
            }
        });
    });

</script>
</body>
</html>